<?php


namespace XinYaTable\Forms\Repositories;


use XinYaTable\Forms\Exceptions\FormException;
use XinYaTable\Forms\Models\EloquentFormCustomView;
use Illuminate\Support\Facades\Log;

class FormCustomViewRepository extends BaseRepository
{

    /**
     * @var EloquentFormCustomView
     */
    private $_eloquentFormCustomView;

    public function __construct(EloquentFormCustomView $eloquentFormCustomView)
    {
        parent::__construct();
        $this->_eloquentFormCustomView = $eloquentFormCustomView;
    }

    /**
     * @param int $formId
     *
     * @return mixed
     */
    public function getSystemViewByForm(int $formId)
    {
        return $this->_eloquentFormCustomView
            ->newQuery()
            ->listFields()
            ->orderBy('order')
            ->systemView($formId)
            ->get();
    }

    /**
     * @param int $formId
     * @param int $uid
     *
     * @return mixed
     */
    public function getCustomViewByForm(int $formId, int $uid)
    {
        return $this->_eloquentFormCustomView
            ->newQuery()
            ->listFields()
            ->orderBy('order')
            ->customView($formId, $uid)
            ->get();
    }

    /**
     * @param int $formId
     * @param int $uid
     *
     * @return bool
     */
    public function checkEmptyCustomView(int $formId, int $uid)
    {
        return $this->_eloquentFormCustomView
            ->newQuery()
            ->where('form_id', $formId)
            ->where('uid', $uid)
            ->exists();
    }

    /**
     * @param array $viewList
     * @param int   $formId
     * @param int   $uid
     */
    public function createUserCustomView(array $viewList, int $formId, int $uid)
    {
        $fillAbleFields = $this->_eloquentFormCustomView->getFillable();
        if (!empty($viewList)) {
            foreach ($viewList as &$viewInfo) {
                $viewInfo['uid']            = $uid;
                $viewInfo['form_id']        = $formId;
                $viewInfo['display_format'] = $viewInfo['display_format'] ?? '';
                $viewInfo['created_at']     = $this->_mTime;
                $viewInfo['updated_at']     = $this->_mTime;
    
                //如果为不可填充的数据则剔除
                foreach ($viewInfo as $viewField => $viewValue) {
                    if (!in_array($viewField, $fillAbleFields)) {
                        unset($viewInfo[$viewField]);
                    }
                }
            }
            $this->_eloquentFormCustomView->newQuery()->insert($viewList);
        }
    }

    /**
     * @param array $viewList
     * @param int   $formId
     * @param int   $uid
     *
     * @return bool
     * @throws FormException
     */
    public function updateUserCustomView(array $viewList, int $formId, int $uid)
    {
        if (!empty($viewList)) {
            $_connection = $this->_eloquentFormCustomView->getConnection();
            $_connection->beginTransaction();
            try {
                $this->_eloquentFormCustomView->newQuery()->customView($formId, $uid)->delete();
                $this->createUserCustomView($viewList, $formId, $uid);
            } catch (\Exception $e) {
                $_connection->rollBack();
                Log::error($e->getMessage(), $e->getTrace());
                throw new FormException(1020001);
            }
            $_connection->commit();

            return true;
        }

        return false;
    }

    /**
     * @param int $id
     *
     * @return mixed
     */
    public function getCustomViewById(int $id)
    {
        return $this->_eloquentFormCustomView
            ->newQuery()
            ->where('id', $id)
            ->first();
    }

    /**
     * @param int $id
     *
     * @return mixed
     * @throws FormException
     */
    public function getCustomViewByIdOrFail(int $id)
    {
        $customViewInfo = $this->getCustomViewById($id);
        if (is_null($customViewInfo)) {
            throw new FormException(1020005);
        }

        return $customViewInfo;
    }

    /**
     * @param int   $id
     * @param array $info
     * @param null  $customInfo
     *
     * @return mixed|null
     * @throws FormException
     */
    public function updateCustomView(int $id, array $info, $customInfo = null)
    {
        if (is_null($customInfo)) {
            $customInfo = $this->getCustomViewByIdOrFail($id);
        }
        $customInfo->fill($info);
        $customInfo->save();

        return $customInfo;
    }

}